package LeetCode刷题;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-09-07 21:22
 * @description: Functions of this class is
 **/
public class 汉明距离 {
    public static void main(String args[]) {
        System.out.println(hammingDistance(1, 4 ));
        //3 0011
        //1 0001
        // 93 73 =2
    }
    public static int hammingDistance(int x, int y) {
        int sum=0;
        //找出两个数字二进制中数字不同的位数
        int ans=x^y;
        while (ans!=0){
            if((ans&1)==1){//找到两数异或得出的数字中为1的数字
                sum++;
            }
            ans>>=1;
        }
        return sum;
    }
    /**
     * 用Java内置的计数器来计算
     * class Solution {
     *     public int hammingDistance(int x, int y) {
     *         return Integer.bitCount(x ^ y);
     *     }
     * }
     *
     */

}